﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions;
using CrystalDecisions.Enterprise;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;
using TI_BAL;
using CrystalDecisions.Shared;

namespace SecuritySystem.Reports.Datasets
{
    public partial class ReportFormCustomer : Form
    {
        TIEntities dbContext;
        public string ReportType = "";
        int ACCOUNT_RECIEVABLE_ID = 4;

        public ReportFormCustomer()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            FillCustomerCombo();
        }

        private void FillCustomerCombo()
        {

            List<Customer> CustomerList = (from pt in dbContext.Customers select pt).ToList<Customer>();
            if (CustomerList != null)
            {
                BindDataWithCustomerCombo(CustomerList);
            }
            else
            {
                CustomerList = new List<Customer>();
            }

        }

        private void BindDataWithCustomerCombo(List<Customer> CustomerList)
        {

            this.comboBoxCustomers.DataSource = null;
            comboBoxCustomers.DisplayMember = "CustomerCompanyName";
            comboBoxCustomers.ValueMember = "ID";
            comboBoxCustomers.DataSource = CustomerList;
            comboBoxCustomers.Refresh();
        }

        private void ReportFormCustomer_Load(object sender, EventArgs e)
        {
            this.dateTimePickerEnd.Value = DateTime.Now;
            this.dateTimePickerStart.Value = DateTime.Now.Subtract(TimeSpan.FromDays(30));  
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
                try
            {
                DateTime tempDate = this.dateTimePickerStart.Value.Subtract(TimeSpan.FromDays(1));
                DateTime dtStart = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);

                tempDate = this.dateTimePickerEnd.Value.AddDays(1);
                DateTime dtEnd = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);

                int customerId = int.Parse(this.comboBoxCustomers.SelectedValue.ToString());
                //var listOfSuppliers = (from sup in dbContext.Suppliers where sup.ContactPersonName != "--" select new { sup.ContactPersonName, sup.CompanyName, sup.SupplierPhoneNumber, sup.SupplierEmailAddress, sup.City, sup.SupplierAddress }).ToList();
                //var ledgars = (from journal in dbContext.Journals where journal.Account_ID == ACCOUNT_RECIEVABLE_ID && journal.Customer_ID == customerId select new { journal.Journal_Date, journal.Customer.CustomerCompanyName, journal.Debit_Amount, journal.Credit_Amount }).ToList();
                var ledgars = (from journal in dbContext.Journals
                               where journal.Journal_Date >= dtStart && journal.Journal_Date <= dtEnd && journal.Account_ID == ACCOUNT_RECIEVABLE_ID && journal.Customer_ID == customerId
                               select new
                               {
                                   Journal_Date = journal.Journal_Date.Value,
                                   Journal_Description = journal.Journal_Description,
                                   CustomerCompanyName = journal.Customer.CustomerCompanyName,
                                   Debit_Amount = journal.Debit_Amount.Value,
                                   Credit_Amount = journal.Credit_Amount.Value, 
                                   Product_Type = journal.Product_Type ?? " ", Sub_Type = journal.Sub_Type ?? " ",Product_Size = journal.Product_Size ?? " ", Quantity = journal.Quantity ?? 0, Per_Item_Price = journal.Per_Item_Price ?? 0}).ToList();


                CustomerLedgerReport reportobj = new CustomerLedgerReport();

                reportobj.SetDataSource(ledgars); ParameterFieldDefinitions crParameterFieldDefinitions;
                ParameterFieldDefinition crParameterFieldDefinition;
                ParameterValues crParameterValues = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition = crParameterFieldDefinitions[0];
                crParameterValues = crParameterFieldDefinition.CurrentValues;

                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                ParameterFieldDefinitions crParameterFieldDefinitions1;
                ParameterFieldDefinition crParameterFieldDefinition1;
                ParameterValues crParameterValues1 = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                crParameterValues1.Clear();
                crParameterValues1.Add(crParameterDiscreteValue1);
                crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);



                this.crystalReportViewer1.ReportSource = reportobj;
            }
            catch { MessageBox.Show("Some Error Occured while generating Report"); }
        

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime tempDate = this.dateTimePickerStart.Value.Subtract(TimeSpan.FromDays(1));
                DateTime dtStart = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);

                tempDate = this.dateTimePickerEnd.Value.AddDays(1);
                DateTime dtEnd = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day);

                //var listOfSuppliers = (from sup in dbContext.Suppliers where sup.ContactPersonName != "--" select new { sup.ContactPersonName, sup.CompanyName, sup.SupplierPhoneNumber, sup.SupplierEmailAddress, sup.City, sup.SupplierAddress }).ToList();
                var ledgars = (from journal in dbContext.Journals
                               where journal.Journal_Date >= dtStart && journal.Journal_Date <= dtEnd && journal.Account_ID == ACCOUNT_RECIEVABLE_ID
                               select new 
                { Journal_Date = journal.Journal_Date.Value,
                  Journal_Description = journal.Journal_Description,
                    CustomerCompanyName = journal.Customer.CustomerCompanyName,
                    Debit_Amount = journal.Debit_Amount.Value, Credit_Amount=journal.Credit_Amount.Value,
                    Product_Type = journal.Product_Type ?? " ", Sub_Type = journal.Sub_Type ?? " ",Product_Size = journal.Product_Size ?? " ", Quantity = journal.Quantity ?? 0, Per_Item_Price = journal.Per_Item_Price ?? 0}).ToList();


                CustomerLedgerReport reportobj = new CustomerLedgerReport();

                reportobj.SetDataSource(ledgars); ParameterFieldDefinitions crParameterFieldDefinitions;
                ParameterFieldDefinition crParameterFieldDefinition;
                ParameterValues crParameterValues = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition = crParameterFieldDefinitions[0];
                crParameterValues = crParameterFieldDefinition.CurrentValues;

                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                ParameterFieldDefinitions crParameterFieldDefinitions1;
                ParameterFieldDefinition crParameterFieldDefinition1;
                ParameterValues crParameterValues1 = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                crParameterValues1.Clear();
                crParameterValues1.Add(crParameterDiscreteValue1);
                crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);



                this.crystalReportViewer1.ReportSource = reportobj;
            }
            catch { MessageBox.Show("Some Error Occured while generating Report"); }
        }
    }
}
